----
title: "Testing the significance of the counterfactual object classification in hadvdid ROI and the relationship between classificaiton and behavioral response"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r load libraries echo=FALSE}
library(readr)
library(reshape2)
library(plyr)
library(ggplot2)
library(mediation)
library(matrixStats)
library(tidyverse)
library(optimx)

```

```{r import echo=FALSE}

#load both cleaned csvs from matlab and add behavioral data

#import data 
univ_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/hadvdid_subjdata_voxp001.csv", 
    col_names = TRUE)

#import ttest data for selecting subjects who considered counterfactuals when making force judgments.
behave_ttest_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/behavioral_results/ttest_results_table.csv", 
    col_names = TRUE)

#merge these datasets
full_univ <- merge(univ_data, behave_ttest_data, "sub")

#isolate the key behavioral data
difference_score=full_univ$difference
subj = full_univ$sub
diff_tstat = full_univ$tstat
diff_pvalue = full_univ$pvalue

roi1_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi1_classification_data.csv", 
    col_names = TRUE)
#combine the roi data with the behavioral data
roi1_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi1_data)


roi2_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi2_classification_data.csv", 
    col_names = TRUE)
roi2_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi2_data)

roi3_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi3_classification_data.csv", 
    col_names = TRUE)
roi3_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi3_data)

roi4_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi4_classification_data.csv", 
    col_names = TRUE)
roi4_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi4_data)

roi5_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi5_classification_data.csv", 
    col_names = TRUE)
roi5_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi5_data)

roi6_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi6_classification_data.csv", 
    col_names = TRUE)
roi6_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi6_data)

roi7_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi7_classification_data.csv", 
    col_names = TRUE)
 roi7_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi7_data)

roi8_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi8_classification_data.csv", 
    col_names = TRUE)
roi8_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi8_data) 

roi9_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi9_classification_data.csv", 
    col_names = TRUE)
roi9_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi9_data)

roi10_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi10_classification_data.csv", 
    col_names = TRUE)
roi10_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi10_data)

roi11_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi11_classification_data.csv", 
    col_names = TRUE)
roi11_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi11_data)

roi12_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi12_classification_data.csv", 
    col_names = TRUE)
roi12_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi12_data)

roi13_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi13_classification_data.csv", 
    col_names = TRUE)
roi13_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi13_data)

roi14_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/roi14_classification_data.csv", 
    col_names = TRUE)
roi14_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, roi14_data)

allrois_data <- read_csv("~/Desktop/Studies/DesertIsland/analyses/univariate/allrois_classification_data.csv", 
    col_names = TRUE)
allrois_data<-cbind(subj, difference_score, diff_tstat, diff_pvalue, allrois_data)

```
#1. For each ROI, gather the data across conditions into a single data frame
```{r clean_data}
###ROI 1
#create a new data frame that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi1_allconds_acc<-gather(roi1_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi1_allconds_acc=roi1_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi1_allconds_acc<-as.data.frame(roi1_allconds_acc)
roi1_allconds_acc$condition=factor(roi1_allconds_acc$condition)
roi1_allconds_acc$subj=factor(roi1_allconds_acc$subj)
#zscore accuracies for future analyses
roi1_allconds_acc$acc_cent<-scale(roi1_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 2
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi2_allconds_acc<-gather(roi2_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi2_allconds_acc=roi2_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi2_allconds_acc<-as.data.frame(roi2_allconds_acc)
roi2_allconds_acc$condition=factor(roi2_allconds_acc$condition)
roi2_allconds_acc$subj=factor(roi2_allconds_acc$subj)
#zscore accuracies for future analyses
roi2_allconds_acc$acc_cent<-scale(roi2_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 3
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi3_allconds_acc<-gather(roi3_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi3_allconds_acc=roi3_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi3_allconds_acc<-as.data.frame(roi3_allconds_acc)
roi3_allconds_acc$condition=factor(roi3_allconds_acc$condition)
roi3_allconds_acc$subj=factor(roi3_allconds_acc$subj)
#zscore accuracies for future analyses
roi3_allconds_acc$acc_cent<-scale(roi3_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 4
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi4_allconds_acc<-gather(roi4_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi4_allconds_acc=roi4_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi4_allconds_acc<-as.data.frame(roi4_allconds_acc)
roi4_allconds_acc$condition=factor(roi4_allconds_acc$condition)
roi4_allconds_acc$subj=factor(roi4_allconds_acc$subj)
#zscore accuracies for future analyses
roi4_allconds_acc$acc_cent<-scale(roi4_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 5
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi5_allconds_acc<-gather(roi5_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi5_allconds_acc=roi5_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi5_allconds_acc<-as.data.frame(roi5_allconds_acc)
roi5_allconds_acc$condition=factor(roi5_allconds_acc$condition)
roi5_allconds_acc$subj=factor(roi5_allconds_acc$subj)
#zscore accuracies for future analyses
roi5_allconds_acc$acc_cent<-scale(roi5_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 6
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi6_allconds_acc<-gather(roi6_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi6_allconds_acc=roi6_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi6_allconds_acc<-as.data.frame(roi6_allconds_acc)
roi6_allconds_acc$condition=factor(roi6_allconds_acc$condition)
roi6_allconds_acc$subj=factor(roi6_allconds_acc$subj)
#zscore accuracies for future analyses
roi6_allconds_acc$acc_cent<-scale(roi6_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 7
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi7_allconds_acc<-gather(roi7_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi7_allconds_acc=roi7_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi7_allconds_acc<-as.data.frame(roi7_allconds_acc)
roi7_allconds_acc$condition=factor(roi7_allconds_acc$condition)
roi7_allconds_acc$subj=factor(roi7_allconds_acc$subj)
#zscore accuracies for future analyses
roi7_allconds_acc$acc_cent<-scale(roi7_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 8
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi8_allconds_acc<-gather(roi8_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi8_allconds_acc=roi8_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi8_allconds_acc<-as.data.frame(roi8_allconds_acc)
roi8_allconds_acc$condition=factor(roi8_allconds_acc$condition)
roi8_allconds_acc$subj=factor(roi8_allconds_acc$subj)
#zscore accuracies for future analyses
roi8_allconds_acc$acc_cent<-scale(roi8_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 9
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi9_allconds_acc<-gather(roi9_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi9_allconds_acc=roi9_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi9_allconds_acc<-as.data.frame(roi9_allconds_acc)
roi9_allconds_acc$condition=factor(roi9_allconds_acc$condition)
roi9_allconds_acc$subj=factor(roi9_allconds_acc$subj)
#zscore accuracies for future analyses
roi9_allconds_acc$acc_cent<-scale(roi9_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 10
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi10_allconds_acc<-gather(roi10_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi10_allconds_acc=roi10_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi10_allconds_acc<-as.data.frame(roi10_allconds_acc)
roi10_allconds_acc$condition=factor(roi10_allconds_acc$condition)
roi10_allconds_acc$subj=factor(roi10_allconds_acc$subj)
#zscore accuracies for future analyses
roi10_allconds_acc$acc_cent<-scale(roi10_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 11
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi11_allconds_acc<-gather(roi11_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi11_allconds_acc=roi11_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi11_allconds_acc<-as.data.frame(roi11_allconds_acc)
roi11_allconds_acc$condition=factor(roi11_allconds_acc$condition)
roi11_allconds_acc$subj=factor(roi11_allconds_acc$subj)
#zscore accuracies for future analyses
roi11_allconds_acc$acc_cent<-scale(roi11_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 12
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi12_allconds_acc<-gather(roi12_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi12_allconds_acc=roi12_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi12_allconds_acc<-as.data.frame(roi12_allconds_acc)
roi12_allconds_acc$condition=factor(roi12_allconds_acc$condition)
roi12_allconds_acc$subj=factor(roi12_allconds_acc$subj)
#zscore accuracies for future analyses
roi12_allconds_acc$acc_cent<-scale(roi12_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 13
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi13_allconds_acc<-gather(roi13_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi13_allconds_acc=roi13_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi13_allconds_acc<-as.data.frame(roi13_allconds_acc)
roi13_allconds_acc$condition=factor(roi13_allconds_acc$condition)
roi13_allconds_acc$subj=factor(roi13_allconds_acc$subj)
#zscore accuracies for future analyses
roi13_allconds_acc$acc_cent<-scale(roi13_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ROI 14
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
roi14_allconds_acc<-gather(roi14_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#roi14_allconds_acc=roi14_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
roi14_allconds_acc<-as.data.frame(roi14_allconds_acc)
roi14_allconds_acc$condition=factor(roi14_allconds_acc$condition)
roi14_allconds_acc$subj=factor(roi14_allconds_acc$subj)
#zscore accuracies for future analyses
roi14_allconds_acc$acc_cent<-scale(roi14_allconds_acc$accuracies, center = TRUE, scale = FALSE)

###ALL ROIS
#create a new dataframe that has one column for average accuracy scores and one row for every subject for every condition (targ_had, targ_did, third_had, third_did) 
#use 9:11 to exclude third_did
allrois_allconds_acc<-gather(allrois_data,condition,accuracies,avg_targ_had_acc:avg_third_did_acc)
#isolate just the subject number, behavioral data, accuracy score, and condition into a single df
#use 22:25 to exclude third did
#allrois_allconds_acc=allrois_allconds_acc[c(21:24)]
#specify that condition is a factor and set the levels
allrois_allconds_acc<-as.data.frame(allrois_allconds_acc)
allrois_allconds_acc$condition=factor(allrois_allconds_acc$condition)
allrois_allconds_acc$subj=factor(allrois_allconds_acc$subj)
#zscore accuracies for future analyses
allrois_allconds_acc$acc_cent<-scale(allrois_allconds_acc$accuracies, center = TRUE, scale = FALSE)

```
#2. In each ROI, test the effect of condition (classifying relevant object in had trials, relevant object in did trials, irrelevant object in had trials, irrelevant object in did trials) on classification accuracy. Because the contrast of interest is classification in targ_had trials compared to the others, use a Helmert contrast with targ_had as the last level. The third contrast from this analysis will compare targ_had to the average of the other three.
```{r condition predicting classification accuracy within ROI}
#In each ROI condition predicting classification accuracy with a random effect for subject using a helmert contrast to compare the effect of targ_had to the average of the other three effects

##ROI 1
#recode condition so that targ had is the last level for helmert contrast
roi1_allconds_acc <- roi1_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi1_allconds_acc$condition_helm=factor(roi1_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi1_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi1_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi1_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi1_allconds_acc)
summary(roi1_fullmod1)

#run the reduced model without condition and compare them
roi1_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi1_allconds_acc)
summary(roi1_noCond_mod1)
anova(roi1_fullmod1,roi1_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi1_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi1_allconds_acc[roi1_allconds_acc$diff_pvalue < .05,])
summary(roi1_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi1_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi1_allconds_acc[roi1_allconds_acc$diff_pvalue < .05,])
summary(roi1_noCond_mod1_goodsubs)
anova(roi1_fullmod1_goodsubs,roi1_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi1_allconds_acc$bad_good <- factor(ifelse(roi1_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi1_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi1_allconds_acc[which(roi1_allconds_acc$bad_good == "good") ,])
summary(roi1_fullmod1_bestsubs)

roi1_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi1_allconds_acc[which(roi1_allconds_acc$bad_good == "good") ,])
summary(roi1_noCond_mod1_bestsubs)
anova(roi1_fullmod1_bestsubs,roi1_noCond_mod1_bestsubs)


##ROI 2
#recode condition so that targ had is the last level for helmert contrast
roi2_allconds_acc <- roi2_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi2_allconds_acc$condition_helm=factor(roi2_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi2_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi2_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi2_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi2_allconds_acc)
summary(roi2_fullmod1)

#run the reduced model without condition and compare them
roi2_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi2_allconds_acc)
summary(roi2_noCond_mod1)
anova(roi2_fullmod1,roi2_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi2_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi2_allconds_acc[roi2_allconds_acc$diff_pvalue < .05,])
summary(roi2_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi2_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi2_allconds_acc[roi2_allconds_acc$diff_pvalue < .05,])
summary(roi2_noCond_mod1_goodsubs)
anova(roi2_fullmod1_goodsubs,roi2_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi2_allconds_acc$bad_good <- factor(ifelse(roi2_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi2_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi2_allconds_acc[which(roi2_allconds_acc$bad_good == "good") ,])
summary(roi2_fullmod1_bestsubs)

roi2_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi2_allconds_acc[which(roi2_allconds_acc$bad_good == "good") ,])
summary(roi2_noCond_mod1_bestsubs)
anova(roi2_fullmod1_bestsubs,roi2_noCond_mod1_bestsubs)

##ROI 3
#recode condition so that targ had is the last level for helmert contrast
roi3_allconds_acc <- roi3_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi3_allconds_acc$condition_helm=factor(roi3_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi3_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi3_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi3_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi3_allconds_acc)
summary(roi3_fullmod1)

#run the reduced model without condition and compare them
roi3_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi3_allconds_acc)
summary(roi3_noCond_mod1)
anova(roi3_fullmod1,roi3_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi3_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi3_allconds_acc[roi3_allconds_acc$diff_pvalue < .05,])
summary(roi3_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi3_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi3_allconds_acc[roi3_allconds_acc$diff_pvalue < .05,])
summary(roi3_noCond_mod1_goodsubs)
anova(roi3_fullmod1_goodsubs,roi3_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi3_allconds_acc$bad_good <- factor(ifelse(roi3_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi3_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi3_allconds_acc[which(roi3_allconds_acc$bad_good == "good") ,])
summary(roi3_fullmod1_bestsubs)

roi3_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi3_allconds_acc[which(roi3_allconds_acc$bad_good == "good") ,])
summary(roi3_noCond_mod1_bestsubs)
anova(roi3_fullmod1_bestsubs,roi3_noCond_mod1_bestsubs)

##ROI 4
#recode condition so that targ had is the last level for helmert contrast
roi4_allconds_acc <- roi4_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi4_allconds_acc$condition_helm=factor(roi4_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi4_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi4_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi4_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi4_allconds_acc)
summary(roi4_fullmod1)

#run the reduced model without condition and compare them
roi4_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi4_allconds_acc)
summary(roi4_noCond_mod1)
anova(roi4_fullmod1,roi4_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi4_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi4_allconds_acc[roi4_allconds_acc$diff_pvalue < .05,])
summary(roi4_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi4_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi4_allconds_acc[roi4_allconds_acc$diff_pvalue < .05,])
summary(roi4_noCond_mod1_goodsubs)
anova(roi4_fullmod1_goodsubs,roi4_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi4_allconds_acc$bad_good <- factor(ifelse(roi4_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi4_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi4_allconds_acc[which(roi4_allconds_acc$bad_good == "good") ,])
summary(roi4_fullmod1_bestsubs)

roi4_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi4_allconds_acc[which(roi4_allconds_acc$bad_good == "good") ,])
summary(roi4_noCond_mod1_bestsubs)
anova(roi4_fullmod1_bestsubs,roi4_noCond_mod1_bestsubs)

##ROI 5
#recode condition so that targ had is the last level for helmert contrast
roi5_allconds_acc <- roi5_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi5_allconds_acc$condition_helm=factor(roi5_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi5_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi5_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi5_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi5_allconds_acc)
summary(roi5_fullmod1)

#run the reduced model without condition and compare them
roi5_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi5_allconds_acc)
summary(roi5_noCond_mod1)
anova(roi5_fullmod1,roi5_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi5_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi5_allconds_acc[roi5_allconds_acc$diff_pvalue < .05,])
summary(roi5_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi5_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi5_allconds_acc[roi5_allconds_acc$diff_pvalue < .05,])
summary(roi5_noCond_mod1_goodsubs)
anova(roi5_fullmod1_goodsubs,roi5_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi5_allconds_acc$bad_good <- factor(ifelse(roi5_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi5_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi5_allconds_acc[which(roi5_allconds_acc$bad_good == "good") ,])
summary(roi5_fullmod1_bestsubs)

roi5_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi5_allconds_acc[which(roi5_allconds_acc$bad_good == "good") ,])
summary(roi5_noCond_mod1_bestsubs)
anova(roi5_fullmod1_bestsubs,roi5_noCond_mod1_bestsubs)

##ROI 6
#recode condition so that targ had is the last level for helmert contrast
roi6_allconds_acc <- roi6_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi6_allconds_acc$condition_helm=factor(roi6_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi6_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi6_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi6_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi6_allconds_acc)
summary(roi6_fullmod1)

#run the reduced model without condition and compare them
roi6_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi6_allconds_acc)
summary(roi6_noCond_mod1)
anova(roi6_fullmod1,roi6_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi6_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi6_allconds_acc[roi6_allconds_acc$diff_pvalue < .05,])
summary(roi6_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi6_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi6_allconds_acc[roi6_allconds_acc$diff_pvalue < .05,])
summary(roi6_noCond_mod1_goodsubs)
anova(roi6_fullmod1_goodsubs,roi6_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi6_allconds_acc$bad_good <- factor(ifelse(roi6_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi6_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi6_allconds_acc[which(roi6_allconds_acc$bad_good == "good") ,])
summary(roi6_fullmod1_bestsubs)

roi6_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi6_allconds_acc[which(roi6_allconds_acc$bad_good == "good") ,])
summary(roi6_noCond_mod1_bestsubs)
anova(roi6_fullmod1_bestsubs,roi6_noCond_mod1_bestsubs)

##ROI 7
#recode condition so that targ had is the last level for helmert contrast
roi7_allconds_acc <- roi7_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi7_allconds_acc$condition_helm=factor(roi7_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi7_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi7_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi7_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi7_allconds_acc)
summary(roi7_fullmod1)

#run the reduced model without condition and compare them
roi7_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi7_allconds_acc)
summary(roi7_noCond_mod1)
anova(roi7_fullmod1,roi7_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi7_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi7_allconds_acc[roi7_allconds_acc$diff_pvalue < .05,])
summary(roi7_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi7_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi7_allconds_acc[roi7_allconds_acc$diff_pvalue < .05,])
summary(roi7_noCond_mod1_goodsubs)
anova(roi7_fullmod1_goodsubs,roi7_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi7_allconds_acc$bad_good <- factor(ifelse(roi7_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi7_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi7_allconds_acc[which(roi7_allconds_acc$bad_good == "good") ,])
summary(roi7_fullmod1_bestsubs)

roi7_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi7_allconds_acc[which(roi7_allconds_acc$bad_good == "good") ,])
summary(roi7_noCond_mod1_bestsubs)
anova(roi7_fullmod1_bestsubs,roi7_noCond_mod1_bestsubs)

##ROI 8
#recode condition so that targ had is the last level for helmert contrast
roi8_allconds_acc <- roi8_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi8_allconds_acc$condition_helm=factor(roi8_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi8_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi8_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi8_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi8_allconds_acc)
summary(roi8_fullmod1)

#run the reduced model without condition and compare them
roi8_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi8_allconds_acc)
summary(roi8_noCond_mod1)
anova(roi8_fullmod1,roi8_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi8_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi8_allconds_acc[roi8_allconds_acc$diff_pvalue < .05,])
summary(roi8_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi8_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi8_allconds_acc[roi8_allconds_acc$diff_pvalue < .05,])
summary(roi8_noCond_mod1_goodsubs)
anova(roi8_fullmod1_goodsubs,roi8_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi8_allconds_acc$bad_good <- factor(ifelse(roi8_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi8_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi8_allconds_acc[which(roi8_allconds_acc$bad_good == "good") ,])
summary(roi8_fullmod1_bestsubs)

roi8_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi8_allconds_acc[which(roi8_allconds_acc$bad_good == "good") ,])
summary(roi8_noCond_mod1_bestsubs)
anova(roi8_fullmod1_bestsubs,roi8_noCond_mod1_bestsubs)

##ROI 9
#recode condition so that targ had is the last level for helmert contrast
roi9_allconds_acc <- roi9_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi9_allconds_acc$condition_helm=factor(roi9_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi9_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi9_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi9_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi9_allconds_acc)
summary(roi9_fullmod1)

#run the reduced model without condition and compare them
roi9_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi9_allconds_acc)
summary(roi9_noCond_mod1)
anova(roi9_fullmod1,roi9_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi9_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi9_allconds_acc[roi9_allconds_acc$diff_pvalue < .05,])
summary(roi9_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi9_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi9_allconds_acc[roi9_allconds_acc$diff_pvalue < .05,])
summary(roi9_noCond_mod1_goodsubs)
anova(roi9_fullmod1_goodsubs,roi9_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi9_allconds_acc$bad_good <- factor(ifelse(roi9_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi9_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi9_allconds_acc[which(roi9_allconds_acc$bad_good == "good") ,])
summary(roi9_fullmod1_bestsubs)

roi9_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi9_allconds_acc[which(roi9_allconds_acc$bad_good == "good") ,])
summary(roi9_noCond_mod1_bestsubs)
anova(roi9_fullmod1_bestsubs,roi9_noCond_mod1_bestsubs)

##ROI 10
#recode condition so that targ had is the last level for helmert contrast
roi10_allconds_acc <- roi10_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi10_allconds_acc$condition_helm=factor(roi10_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi10_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi10_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi10_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi10_allconds_acc)
summary(roi10_fullmod1)

#run the reduced model without condition and compare them
roi10_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi10_allconds_acc)
summary(roi10_noCond_mod1)
anova(roi10_fullmod1,roi10_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi10_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi10_allconds_acc[roi10_allconds_acc$diff_pvalue < .05,])
summary(roi10_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi10_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi10_allconds_acc[roi10_allconds_acc$diff_pvalue < .05,])
summary(roi10_noCond_mod1_goodsubs)
anova(roi10_fullmod1_goodsubs,roi10_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi10_allconds_acc$bad_good <- factor(ifelse(roi10_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi10_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi10_allconds_acc[which(roi10_allconds_acc$bad_good == "good") ,])
summary(roi10_fullmod1_bestsubs)

roi10_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi10_allconds_acc[which(roi10_allconds_acc$bad_good == "good") ,])
summary(roi10_noCond_mod1_bestsubs)
anova(roi10_fullmod1_bestsubs,roi10_noCond_mod1_bestsubs)

##ROI 11
#recode condition so that targ had is the last level for helmert contrast
roi11_allconds_acc <- roi11_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi11_allconds_acc$condition_helm=factor(roi11_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi11_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi11_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi11_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi11_allconds_acc)
summary(roi11_fullmod1)

#run the reduced model without condition and compare them
roi11_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi11_allconds_acc)
summary(roi11_noCond_mod1)
anova(roi11_fullmod1,roi11_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi11_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi11_allconds_acc[roi11_allconds_acc$diff_pvalue < .05,])
summary(roi11_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi11_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi11_allconds_acc[roi11_allconds_acc$diff_pvalue < .05,])
summary(roi11_noCond_mod1_goodsubs)
anova(roi11_fullmod1_goodsubs,roi11_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi11_allconds_acc$bad_good <- factor(ifelse(roi11_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi11_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi11_allconds_acc[which(roi11_allconds_acc$bad_good == "good") ,])
summary(roi11_fullmod1_bestsubs)

roi11_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi11_allconds_acc[which(roi11_allconds_acc$bad_good == "good") ,])
summary(roi11_noCond_mod1_bestsubs)
anova(roi11_fullmod1_bestsubs,roi11_noCond_mod1_bestsubs)

##ROI 12
#recode condition so that targ had is the last level for helmert contrast
roi12_allconds_acc <- roi12_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi12_allconds_acc$condition_helm=factor(roi12_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi12_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi12_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi12_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi12_allconds_acc)
summary(roi12_fullmod1)

#run the reduced model without condition and compare them
roi12_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi12_allconds_acc)
summary(roi12_noCond_mod1)
anova(roi12_fullmod1,roi12_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi12_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi12_allconds_acc[roi12_allconds_acc$diff_pvalue < .05,])
summary(roi12_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi12_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi12_allconds_acc[roi12_allconds_acc$diff_pvalue < .05,])
summary(roi12_noCond_mod1_goodsubs)
anova(roi12_fullmod1_goodsubs,roi12_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi12_allconds_acc$bad_good <- factor(ifelse(roi12_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi12_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi12_allconds_acc[which(roi12_allconds_acc$bad_good == "good") ,])
summary(roi12_fullmod1_bestsubs)

roi12_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi12_allconds_acc[which(roi12_allconds_acc$bad_good == "good") ,])
summary(roi12_noCond_mod1_bestsubs)
anova(roi12_fullmod1_bestsubs,roi12_noCond_mod1_bestsubs)

##ROI 13
#recode condition so that targ had is the last level for helmert contrast
roi13_allconds_acc <- roi13_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi13_allconds_acc$condition_helm=factor(roi13_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi13_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi13_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi13_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi13_allconds_acc)
summary(roi13_fullmod1)

#run the reduced model without condition and compare them
roi13_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi13_allconds_acc)
summary(roi13_noCond_mod1)
anova(roi13_fullmod1,roi13_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi13_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi13_allconds_acc[roi13_allconds_acc$diff_pvalue < .05,])
summary(roi13_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi13_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi13_allconds_acc[roi13_allconds_acc$diff_pvalue < .05,])
summary(roi13_noCond_mod1_goodsubs)
anova(roi13_fullmod1_goodsubs,roi13_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi13_allconds_acc$bad_good <- factor(ifelse(roi13_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi13_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi13_allconds_acc[which(roi13_allconds_acc$bad_good == "good") ,])
summary(roi13_fullmod1_bestsubs)

roi13_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi13_allconds_acc[which(roi13_allconds_acc$bad_good == "good") ,])
summary(roi13_noCond_mod1_bestsubs)
anova(roi13_fullmod1_bestsubs,roi13_noCond_mod1_bestsubs)

##ROI 14
#recode condition so that targ had is the last level for helmert contrast
roi14_allconds_acc <- roi14_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
roi14_allconds_acc$condition_helm=factor(roi14_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(roi14_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(roi14_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
roi14_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi14_allconds_acc)
summary(roi14_fullmod1)

#run the reduced model without condition and compare them
roi14_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = roi14_allconds_acc)
summary(roi14_noCond_mod1)
anova(roi14_fullmod1,roi14_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
roi14_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=roi14_allconds_acc[roi14_allconds_acc$diff_pvalue < .05,])
summary(roi14_fullmod1_goodsubs)

#run the reduced model without condition and compare them
roi14_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi14_allconds_acc[roi14_allconds_acc$diff_pvalue < .05,])
summary(roi14_noCond_mod1_goodsubs)
anova(roi14_fullmod1_goodsubs,roi14_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
roi14_allconds_acc$bad_good <- factor(ifelse(roi14_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

roi14_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = roi14_allconds_acc[which(roi14_allconds_acc$bad_good == "good") ,])
summary(roi14_fullmod1_bestsubs)

roi14_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = roi14_allconds_acc[which(roi14_allconds_acc$bad_good == "good") ,])
summary(roi14_noCond_mod1_bestsubs)
anova(roi14_fullmod1_bestsubs,roi14_noCond_mod1_bestsubs)

##ALL ROIS
#recode condition so that targ had is the last level for helmert contrast
allrois_allconds_acc <- allrois_allconds_acc %>% mutate(
    condition_helm = case_when(
      condition=="avg_targ_had_acc" ~ 4,
      condition=="avg_targ_did_acc" ~ 3,
      condition=="avg_third_had_acc" ~ 2,
      condition=="avg_third_did_acc" ~ 1,
    )
)
#specify that this new variable is a factor
allrois_allconds_acc$condition_helm=factor(allrois_allconds_acc$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(allrois_allconds_acc$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(allrois_allconds_acc$condition_helm)

#run the full model with condition and random effect for subject
allrois_fullmod1<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=allrois_allconds_acc)
summary(allrois_fullmod1)

#run the reduced model without condition and compare them
allrois_noCond_mod1<-lme4::lmer(accuracies ~ (1|subj), data = allrois_allconds_acc)
summary(allrois_noCond_mod1)
anova(allrois_fullmod1,allrois_noCond_mod1)

##Rerun using only subjects who gave significantly higher force judgments when there was no rational alternative than when there was. 
#run the full model with condition and random effect for subject
allrois_fullmod1_goodsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data=allrois_allconds_acc[allrois_allconds_acc$diff_pvalue < .05,])
summary(allrois_fullmod1_goodsubs)

#run the reduced model without condition and compare them
allrois_noCond_mod1_goodsubs<-lme4::lmer(accuracies ~ (1|subj), data = allrois_allconds_acc[allrois_allconds_acc$diff_pvalue < .05,])
summary(allrois_noCond_mod1_goodsubs)
anova(allrois_fullmod1_goodsubs,allrois_noCond_mod1_goodsubs)

#rerun model comparisons using only subjects whose difference scores are above the median (those subjects who most consider the value of the alternatives when making their force judgments)
allrois_allconds_acc$bad_good <- factor(ifelse(allrois_allconds_acc$difference_score > median(difference_score, data = allrois_allconds_acc), "good", "bad"))

allrois_fullmod1_bestsubs<-lme4::lmer(accuracies ~ condition_helm + (1|subj), data = allrois_allconds_acc[which(allrois_allconds_acc$bad_good == "good") ,])
summary(allrois_fullmod1_bestsubs)

allrois_noCond_mod1_bestsubs<-lme4::lmer(accuracies ~ (1|subj), data = allrois_allconds_acc[which(allrois_allconds_acc$bad_good == "good") ,])
summary(allrois_noCond_mod1_bestsubs)
anova(allrois_fullmod1_bestsubs,allrois_noCond_mod1_bestsubs)


```


#3. In each ROI predict behavior (difference in the degree to which subjects agreed an agent was forced when there was no relevant alternative compared to when there was) from accuracy, separately for key condition (targ_had) and the average of the other three conditions. Then compare the betas from the two models using a wald test. We would expect a) that classification accuracy predects subejcts' behavioral data in the key condition significantly better than chance, but not in the other three and b) that classification accuracy predicts subjects' behavioral data in the key condition *better* than in the other three conditions. 

#The correlation comparisons are done with cocor: http://comparingcorrelations.org/ Diedenhofen, B. & Musch, J. (2015). cocor: A Comprehensive Solution for the Statistical Comparison of Correlations. PLoS ONE, 10(4): e0121945. doi:10.1371/journal.pone.0121945
```{r accuracyXcondition interaction predicting behavior}
#In each ROI: predict behavior from accuracy separately for key condition and average of other three conditions. Then compare correlation coefficients using a z test.

##ROI 1
#center the data for the key condition, and average and center the data for the other three conditions
roi1_data$altcond_avg_acc <- rowMeans(roi1_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi1_data$altcond_avg_acc_cent <- scale(roi1_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi1_data$avg_targ_had_acc_cent <- scale(roi1_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi1_data$difference_score, roi1_data$altcond_avg_acc_cent)
cor.test(roi1_data$difference_score, roi1_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi1_data$difference_score, roi1_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi1_data$difference_score, roi1_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi1_data$altcond_avg_acc_cent, roi1_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi1_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 2
roi2_data$altcond_avg_acc <- rowMeans(roi2_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi2_data$altcond_avg_acc_cent <- scale(roi2_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi2_data$avg_targ_had_acc_cent <- scale(roi2_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi2_data$difference_score, roi2_data$altcond_avg_acc_cent)
cor.test(roi2_data$difference_score, roi2_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi2_data$difference_score, roi2_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi2_data$difference_score, roi2_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi2_data$altcond_avg_acc_cent, roi2_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi2_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 3
roi3_data$altcond_avg_acc <- rowMeans(roi3_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi3_data$altcond_avg_acc_cent <- scale(roi3_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi3_data$avg_targ_had_acc_cent <- scale(roi3_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi3_data$difference_score, roi3_data$altcond_avg_acc_cent)
cor.test(roi3_data$difference_score, roi3_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi3_data$difference_score, roi3_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi3_data$difference_score, roi3_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi3_data$altcond_avg_acc_cent, roi3_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi3_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 4
roi4_data$altcond_avg_acc <- rowMeans(roi4_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi4_data$altcond_avg_acc_cent <- scale(roi4_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi4_data$avg_targ_had_acc_cent <- scale(roi4_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi4_data$difference_score, roi4_data$altcond_avg_acc_cent)
cor.test(roi4_data$difference_score, roi4_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi4_data$difference_score, roi4_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi4_data$difference_score, roi4_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi4_data$altcond_avg_acc_cent, roi4_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi4_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 5
roi5_data$altcond_avg_acc <- rowMeans(roi5_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi5_data$altcond_avg_acc_cent <- scale(roi5_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi5_data$avg_targ_had_acc_cent <- scale(roi5_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi5_data$difference_score, roi5_data$altcond_avg_acc_cent)
cor.test(roi5_data$difference_score, roi5_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi5_data$difference_score, roi5_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi5_data$difference_score, roi5_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi5_data$altcond_avg_acc_cent, roi5_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi5_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 6
roi6_data$altcond_avg_acc <- rowMeans(roi6_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi6_data$altcond_avg_acc_cent <- scale(roi6_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi6_data$avg_targ_had_acc_cent <- scale(roi6_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi6_data$difference_score, roi6_data$altcond_avg_acc_cent)
cor.test(roi6_data$difference_score, roi6_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi6_data$difference_score, roi6_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi6_data$difference_score, roi6_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi6_data$altcond_avg_acc_cent, roi6_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi7_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 7
roi7_data$altcond_avg_acc <- rowMeans(roi7_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi7_data$altcond_avg_acc_cent <- scale(roi7_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi7_data$avg_targ_had_acc_cent <- scale(roi7_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi7_data$difference_score, roi7_data$altcond_avg_acc_cent)
cor.test(roi7_data$difference_score, roi7_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi7_data$difference_score, roi7_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi7_data$difference_score, roi7_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi7_data$altcond_avg_acc_cent, roi7_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi7_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 8
roi8_data$altcond_avg_acc <- rowMeans(roi8_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi8_data$altcond_avg_acc_cent <- scale(roi8_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi8_data$avg_targ_had_acc_cent <- scale(roi8_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi8_data$difference_score, roi8_data$altcond_avg_acc_cent)
cor.test(roi8_data$difference_score, roi8_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi8_data$difference_score, roi8_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi8_data$difference_score, roi8_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi8_data$altcond_avg_acc_cent, roi8_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi8_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 9
roi9_data$altcond_avg_acc <- rowMeans(roi9_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi9_data$altcond_avg_acc_cent <- scale(roi9_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi9_data$avg_targ_had_acc_cent <- scale(roi9_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi9_data$difference_score, roi9_data$altcond_avg_acc_cent)
cor.test(roi9_data$difference_score, roi9_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi9_data$difference_score, roi9_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi9_data$difference_score, roi9_data$avg_targ_had_acc_cent)
r.alt_key <- cor(roi9_data$altcond_avg_acc_cent, roi9_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi9_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 10
roi10_data$altcond_avg_acc <- rowMeans(roi10_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi10_data$altcond_avg_acc_cent <- scale(roi10_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi10_data$avg_targ_had_acc_cent <- scale(roi10_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi10_data$difference_score, roi10_data$altcond_avg_acc_cent)
cor.test(roi10_data$difference_score, roi10_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi10_data$difference_score, roi10_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi10_data$difference_score, roi10_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(roi10_data$altcond_avg_acc_cent, roi10_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi10_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 11
roi11_data$altcond_avg_acc <- rowMeans(roi11_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi11_data$altcond_avg_acc_cent <- scale(roi11_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi11_data$avg_targ_had_acc_cent <- scale(roi11_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi11_data$difference_score, roi11_data$altcond_avg_acc_cent)
cor.test(roi11_data$difference_score, roi11_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi11_data$difference_score, roi11_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi11_data$difference_score, roi11_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(roi11_data$altcond_avg_acc_cent, roi11_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi11_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 12
roi12_data$altcond_avg_acc <- rowMeans(roi12_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi12_data$altcond_avg_acc_cent <- scale(roi12_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi12_data$avg_targ_had_acc_cent <- scale(roi12_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi12_data$difference_score, roi12_data$altcond_avg_acc_cent)
cor.test(roi12_data$difference_score, roi12_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi12_data$difference_score, roi12_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi12_data$difference_score, roi12_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(roi12_data$altcond_avg_acc_cent, roi12_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi12_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 13
roi13_data$altcond_avg_acc <- rowMeans(roi13_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi13_data$altcond_avg_acc_cent <- scale(roi13_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi13_data$avg_targ_had_acc_cent <- scale(roi13_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi13_data$difference_score, roi13_data$altcond_avg_acc_cent)
cor.test(roi13_data$difference_score, roi13_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi13_data$difference_score, roi13_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi13_data$difference_score, roi13_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(roi13_data$altcond_avg_acc_cent, roi13_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi13_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ROI 14
roi14_data$altcond_avg_acc <- rowMeans(roi14_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
roi14_data$altcond_avg_acc_cent <- scale(roi14_data$altcond_avg_acc, center = TRUE, scale = FALSE)
roi14_data$avg_targ_had_acc_cent <- scale(roi14_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(roi14_data$difference_score, roi14_data$altcond_avg_acc_cent)
cor.test(roi14_data$difference_score, roi14_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(roi14_data$difference_score, roi14_data$altcond_avg_acc_cent)
r.diff_key <- cor(roi14_data$difference_score, roi14_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(roi14_data$altcond_avg_acc_cent, roi14_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(roi14_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))

##ALL ROIS
allrois_data$altcond_avg_acc <- rowMeans(allrois_data[ , c("avg_targ_did_acc", "avg_third_had_acc", "avg_third_did_acc")], na.rm=TRUE)
allrois_data$altcond_avg_acc_cent <- scale(allrois_data$altcond_avg_acc, center = TRUE, scale = FALSE)
allrois_data$avg_targ_had_acc_cent <- scale(allrois_data$avg_targ_had_acc, center = TRUE, scale = FALSE)

#test for significant correlations
cor.test(allrois_data$difference_score, allrois_data$altcond_avg_acc_cent)
cor.test(allrois_data$difference_score, allrois_data$avg_targ_had_acc_cent)

#compute correlation coefficients for our three different variables (difference score, accuracies in key condition, accuracies in alternative conditions)
r.diff_alt <- cor(allrois_data$difference_score, allrois_data$altcond_avg_acc_cent)
r.diff_key <- cor(allrois_data$difference_score, allrois_data$avg_targ_had_acc_cent) 
r.alt_key <- cor(allrois_data$altcond_avg_acc_cent, allrois_data$avg_targ_had_acc_cent)

#use cocor with dependent overlaping groups to compare correlation coefficients
cocor::cocor.dep.groups.overlap(r.diff_alt[1,1], r.diff_key[1,1], r.alt_key[1,1], length(allrois_data), var.labels=c("difference_score", "altcond_acc", "keycond_acc"))


```

#4. Test the relationship between classification accuracy across ROIs and behavior, comparing across condition. To do this I first predicted behavior from classification accuracy within each ROI and within each condition (classifying relevant objects in had trials, relevant objects in did trials, irrelevant objects in had trials, and irrelevant objects in did trials). From each of these models I collected the slopes creating a new dataset with 2 variables: estimate and condtion. Finally, I used a helmert contrast to compare the slopes in the target had condition to the average of the other three conditions.


```{r linear models for targ had}
#For each ROI predict behavior from classification accuracy of the relevant (targtet) object when subjects are making judgments about what the agent had to do.Then extract the coefficients and save them as a new data object.

#run model for this ROI
roi1mod <- lm(difference_score ~ acc_cent, data = roi1_allconds_acc[ which(roi1_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi1mod)
#create a new data object that is just the slope output for this ROI
roi1mod_coef<-summary(roi1mod)$coefficients[2,] 

roi2mod <- lm(difference_score ~ acc_cent, data = roi2_allconds_acc[ which(roi2_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi2mod)
#create a new data object that is just the slope output for this ROI
roi2mod_coef<-summary(roi2mod)$coefficients[2,] 

roi3mod <- lm(difference_score ~ acc_cent, data = roi3_allconds_acc[ which(roi3_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi3mod)
#create a new data object that is just the slope output for this ROI
roi3mod_coef<-summary(roi3mod)$coefficients[2,] 

roi4mod <- lm(difference_score ~ acc_cent, data = roi4_allconds_acc[ which(roi4_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi4mod)
#create a new data object that is just the slope output for this ROI
roi4mod_coef<-summary(roi4mod)$coefficients[2,] 

roi5mod <- lm(difference_score ~ acc_cent, data = roi5_allconds_acc[ which(roi5_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi5mod)
#create a new data object that is just the slope output for this ROI
roi5mod_coef<-summary(roi5mod)$coefficients[2,] 

roi6mod <- lm(difference_score ~ acc_cent, data = roi6_allconds_acc[ which(roi6_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi6mod)
#create a new data object that is just the slope output for this ROI
roi6mod_coef<-summary(roi6mod)$coefficients[2,] 

roi7mod <- lm(difference_score ~ acc_cent, data = roi7_allconds_acc[ which(roi7_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi7mod)
#create a new data object that is just the slope output for this ROI
roi7mod_coef<-summary(roi7mod)$coefficients[2,] 

roi8mod <- lm(difference_score ~ acc_cent, data = roi8_allconds_acc[ which(roi8_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi8mod)
#create a new data object that is just the slope output for this ROI
roi8mod_coef<-summary(roi8mod)$coefficients[2,] 

roi9mod <- lm(difference_score ~ acc_cent, data = roi9_allconds_acc[ which(roi9_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi9mod)
#create a new data object that is just the slope output for this ROI
roi9mod_coef<-summary(roi9mod)$coefficients[2,] 

roi10mod <- lm(difference_score ~ acc_cent, data = roi10_allconds_acc[ which(roi10_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi10mod)
#create a new data object that is just the slope output for this ROI
roi10mod_coef<-summary(roi10mod)$coefficients[2,] 

roi11mod <- lm(difference_score ~ acc_cent, data = roi11_allconds_acc[ which(roi11_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi11mod)
#create a new data object that is just the slope output for this ROI
roi11mod_coef<-summary(roi11mod)$coefficients[2,] 

roi12mod <- lm(difference_score ~ acc_cent, data = roi12_allconds_acc[ which(roi12_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi12mod)
#create a new data object that is just the slope output for this ROI
roi12mod_coef<-summary(roi12mod)$coefficients[2,] 

roi13mod <- lm(difference_score ~ acc_cent, data = roi13_allconds_acc[ which(roi13_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi13mod)
#create a new data object that is just the slope output for this ROI
roi13mod_coef<-summary(roi13mod)$coefficients[2,] 

roi14mod <- lm(difference_score ~ acc_cent, data = roi14_allconds_acc[ which(roi14_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(roi14mod)
#create a new data object that is just the slope output for this ROI
roi14mod_coef<-summary(roi14mod)$coefficients[2,] 

allroismod <- lm(difference_score ~ acc_cent, data = allrois_allconds_acc[ which(allrois_allconds_acc$condition =='avg_targ_had_acc'), ])
summary(allroismod)
#create a new data object that is just the slope output for this ROI
allroismod_coef<-summary(allroismod)$coefficients[2,] 

```

```{r linear models for targ did}
#For each ROI predict behavior from classification accuracy of the relevant (target) object when subjects are making judgments about what the actually did.Then extract the coefficients and save them as a new data object.
#run model for this ROI
roi1mod_targdid <- lm(difference_score ~ acc_cent, data = roi1_allconds_acc[ which(roi1_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi1mod_targdid)
#create a new data object that is just the slope output for this ROI
roi1mod_targdid_coef<-summary(roi1mod_targdid)$coefficients[2,] 

roi2mod_targdid <- lm(difference_score ~ acc_cent, data = roi2_allconds_acc[ which(roi2_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi2mod_targdid)
#create a new data object that is just the slope output for this ROI
roi2mod_targdid_coef<-summary(roi2mod_targdid)$coefficients[2,] 

roi3mod_targdid <- lm(difference_score ~ acc_cent, data = roi3_allconds_acc[ which(roi3_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi3mod_targdid)
#create a new data object that is just the slope output for this ROI
roi3mod_targdid_coef<-summary(roi3mod_targdid)$coefficients[2,] 

roi4mod_targdid <- lm(difference_score ~ acc_cent, data = roi4_allconds_acc[ which(roi4_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi4mod_targdid)
#create a new data object that is just the slope output for this ROI
roi4mod_targdid_coef<-summary(roi4mod_targdid)$coefficients[2,] 

roi5mod_targdid <- lm(difference_score ~ acc_cent, data = roi5_allconds_acc[ which(roi5_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi5mod_targdid)
#create a new data object that is just the slope output for this ROI
roi5mod_targdid_coef<-summary(roi5mod_targdid)$coefficients[2,] 

roi6mod_targdid <- lm(difference_score ~ acc_cent, data = roi6_allconds_acc[ which(roi6_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi6mod_targdid)
#create a new data object that is just the slope output for this ROI
roi6mod_targdid_coef<-summary(roi6mod_targdid)$coefficients[2,] 

roi7mod_targdid <- lm(difference_score ~ acc_cent, data = roi7_allconds_acc[ which(roi7_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi7mod_targdid)
#create a new data object that is just the slope output for this ROI
roi7mod_targdid_coef<-summary(roi7mod_targdid)$coefficients[2,] 

roi8mod_targdid <- lm(difference_score ~ acc_cent, data = roi8_allconds_acc[ which(roi8_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi8mod_targdid)
#create a new data object that is just the slope output for this ROI
roi8mod_targdid_coef<-summary(roi8mod_targdid)$coefficients[2,] 

roi9mod_targdid <- lm(difference_score ~ acc_cent, data = roi9_allconds_acc[ which(roi9_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi9mod_targdid)
#create a new data object that is just the slope output for this ROI
roi9mod_targdid_coef<-summary(roi9mod_targdid)$coefficients[2,] 

roi10mod_targdid <- lm(difference_score ~ acc_cent, data = roi10_allconds_acc[ which(roi10_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi10mod_targdid)
#create a new data object that is just the slope output for this ROI
roi10mod_targdid_coef<-summary(roi10mod_targdid)$coefficients[2,] 

roi11mod_targdid <- lm(difference_score ~ acc_cent, data = roi11_allconds_acc[ which(roi11_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi11mod_targdid)
#create a new data object that is just the slope output for this ROI
roi11mod_targdid_coef<-summary(roi11mod_targdid)$coefficients[2,] 

roi12mod_targdid <- lm(difference_score ~ acc_cent, data = roi12_allconds_acc[ which(roi12_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi12mod_targdid)
#create a new data object that is just the slope output for this ROI
roi12mod_targdid_coef<-summary(roi12mod_targdid)$coefficients[2,] 

roi13mod_targdid <- lm(difference_score ~ acc_cent, data = roi13_allconds_acc[ which(roi13_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi13mod_targdid)
#create a new data object that is just the slope output for this ROI
roi13mod_targdid_coef<-summary(roi13mod_targdid)$coefficients[2,] 

roi14mod_targdid <- lm(difference_score ~ acc_cent, data = roi14_allconds_acc[ which(roi14_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(roi14mod_targdid)
#create a new data object that is just the slope output for this ROI
roi14mod_targdid_coef<-summary(roi14mod_targdid)$coefficients[2,] 

allroismod_targdid <- lm(difference_score ~ acc_cent, data = allrois_allconds_acc[ which(allrois_allconds_acc$condition =='avg_targ_did_acc'), ])
summary(allroismod_targdid)
#create a new data object that is just the slope output for this ROI
allroismod_targdid_coef<-summary(allroismod_targdid)$coefficients[2,] 

```

```{r linear models for third had}
#For each ROI predict behavior from classification accuracy of the irrelevant (third) object when subjects are making judgments about what the agent had to do.Then extract the coefficients and save them as a new data object.

#run model for this ROI
roi1mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi1_allconds_acc[ which(roi1_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi1mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi1mod_thirdhad_coef<-summary(roi1mod_thirdhad)$coefficients[2,] 

roi2mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi2_allconds_acc[ which(roi2_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi2mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi2mod_thirdhad_coef<-summary(roi2mod_thirdhad)$coefficients[2,] 

roi3mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi3_allconds_acc[ which(roi3_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi3mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi3mod_thirdhad_coef<-summary(roi3mod_thirdhad)$coefficients[2,] 

roi4mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi4_allconds_acc[ which(roi4_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi4mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi4mod_thirdhad_coef<-summary(roi4mod_thirdhad)$coefficients[2,] 

roi5mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi5_allconds_acc[ which(roi5_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi5mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi5mod_thirdhad_coef<-summary(roi5mod_thirdhad)$coefficients[2,] 

roi6mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi6_allconds_acc[ which(roi6_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi6mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi6mod_thirdhad_coef<-summary(roi6mod_thirdhad)$coefficients[2,] 

roi7mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi7_allconds_acc[ which(roi7_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi7mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi7mod_thirdhad_coef<-summary(roi7mod_thirdhad)$coefficients[2,] 

roi8mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi8_allconds_acc[ which(roi8_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi8mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi8mod_thirdhad_coef<-summary(roi8mod_thirdhad)$coefficients[2,] 

roi9mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi9_allconds_acc[ which(roi9_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi9mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi9mod_thirdhad_coef<-summary(roi9mod_thirdhad)$coefficients[2,] 

roi10mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi10_allconds_acc[ which(roi10_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi10mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi10mod_thirdhad_coef<-summary(roi10mod_thirdhad)$coefficients[2,] 

roi11mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi11_allconds_acc[ which(roi11_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi11mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi11mod_thirdhad_coef<-summary(roi11mod_thirdhad)$coefficients[2,] 

roi12mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi12_allconds_acc[ which(roi12_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi12mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi12mod_thirdhad_coef<-summary(roi12mod_thirdhad)$coefficients[2,] 

roi13mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi13_allconds_acc[ which(roi13_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi13mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi13mod_thirdhad_coef<-summary(roi13mod_thirdhad)$coefficients[2,] 

roi14mod_thirdhad <- lm(difference_score ~ acc_cent, data = roi14_allconds_acc[ which(roi14_allconds_acc$condition =='avg_third_had_acc'), ])
summary(roi14mod_thirdhad)
#create a new data object that is just the slope output for this ROI
roi14mod_thirdhad_coef<-summary(roi14mod_thirdhad)$coefficients[2,] 

allroismod_thirdhad <- lm(difference_score ~ acc_cent, data = allrois_allconds_acc[ which(allrois_allconds_acc$condition =='avg_third_had_acc'), ])
summary(allroismod_thirdhad)
#create a new data object that is just the slope output for this ROI
allroismod_thirdhad_coef<-summary(allroismod_thirdhad)$coefficients[2,] 

```

```{r linear models for third did}
#For each ROI predict behavior from classification accuracy of the irrelevant (third) object when subjects are making judgments about what the agent actually did.Then extract the coefficients and save them as a new data object.

#run model for this ROI
roi1mod_thirddid <- lm(difference_score ~ acc_cent, data = roi1_allconds_acc[ which(roi1_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi1mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi1mod_thirddid_coef<-summary(roi1mod_thirddid)$coefficients[2,] 

roi2mod_thirddid <- lm(difference_score ~ acc_cent, data = roi2_allconds_acc[ which(roi2_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi2mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi2mod_thirddid_coef<-summary(roi2mod_thirddid)$coefficients[2,] 

roi3mod_thirddid <- lm(difference_score ~ acc_cent, data = roi3_allconds_acc[ which(roi3_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi3mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi3mod_thirddid_coef<-summary(roi3mod_thirddid)$coefficients[2,] 

roi4mod_thirddid <- lm(difference_score ~ acc_cent, data = roi4_allconds_acc[ which(roi4_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi4mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi4mod_thirddid_coef<-summary(roi4mod_thirddid)$coefficients[2,] 

roi5mod_thirddid <- lm(difference_score ~ acc_cent, data = roi5_allconds_acc[ which(roi5_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi5mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi5mod_thirddid_coef<-summary(roi5mod_thirddid)$coefficients[2,] 

roi6mod_thirddid <- lm(difference_score ~ acc_cent, data = roi6_allconds_acc[ which(roi6_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi6mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi6mod_thirddid_coef<-summary(roi6mod_thirddid)$coefficients[2,] 

roi7mod_thirddid <- lm(difference_score ~ acc_cent, data = roi7_allconds_acc[ which(roi7_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi7mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi7mod_thirddid_coef<-summary(roi7mod_thirddid)$coefficients[2,] 

roi8mod_thirddid <- lm(difference_score ~ acc_cent, data = roi8_allconds_acc[ which(roi8_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi8mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi8mod_thirddid_coef<-summary(roi8mod_thirddid)$coefficients[2,] 

roi9mod_thirddid <- lm(difference_score ~ acc_cent, data = roi9_allconds_acc[ which(roi9_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi9mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi9mod_thirddid_coef<-summary(roi9mod_thirddid)$coefficients[2,] 

roi10mod_thirddid <- lm(difference_score ~ acc_cent, data = roi10_allconds_acc[ which(roi10_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi10mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi10mod_thirddid_coef<-summary(roi10mod_thirddid)$coefficients[2,] 

roi11mod_thirddid <- lm(difference_score ~ acc_cent, data = roi11_allconds_acc[ which(roi11_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi11mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi11mod_thirddid_coef<-summary(roi11mod_thirddid)$coefficients[2,] 

roi12mod_thirddid <- lm(difference_score ~ acc_cent, data = roi12_allconds_acc[ which(roi12_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi12mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi12mod_thirddid_coef<-summary(roi12mod_thirddid)$coefficients[2,] 

roi13mod_thirddid <- lm(difference_score ~ acc_cent, data = roi13_allconds_acc[ which(roi13_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi13mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi13mod_thirddid_coef<-summary(roi13mod_thirddid)$coefficients[2,] 

roi14mod_thirddid <- lm(difference_score ~ acc_cent, data = roi14_allconds_acc[ which(roi14_allconds_acc$condition =='avg_third_did_acc'), ])
summary(roi14mod_thirddid)
#create a new data object that is just the slope output for this ROI
roi14mod_thirddid_coef<-summary(roi14mod_thirddid)$coefficients[2,] 

allroismod_thirddid <- lm(difference_score ~ acc_cent, data = allrois_allconds_acc[ which(allrois_allconds_acc$condition =='avg_third_did_acc'), ])
summary(allroismod_thirddid)
#create a new data object that is just the slope output for this ROI
allroismod_thirddid_coef<-summary(allroismod_thirddid)$coefficients[2,] 

```

#5 Test the effect of condition on the slopes from the above regressions predicting behavior from classification accuracy within each ROI.
```{r test slope across ROI predicted by condition}
#For each condition, combine regression coeffiecients for each model into a single dataset and then compute SD for reporting descriptives
full_coef_data_targhad<-data.frame(rbind(roi1mod_coef, roi2mod_coef, roi3mod_coef, roi4mod_coef, roi5mod_coef, roi6mod_coef, roi7mod_coef, roi8mod_coef, roi9mod_coef, roi10mod_coef, roi11mod_coef, roi12mod_coef, roi13mod_coef, roi14mod_coef))

full_coef_data_targhad$sd <- full_coef_data_targhad$Std..Error * (sqrt(36))

write.csv(full_coef_data_targhad, "full_coef_data_targhad.csv")

full_coef_data_targdid<-data.frame(rbind(roi1mod_targdid_coef, roi2mod_targdid_coef, roi3mod_targdid_coef, roi4mod_targdid_coef, roi5mod_targdid_coef, roi6mod_targdid_coef, roi7mod_targdid_coef, roi8mod_targdid_coef, roi9mod_targdid_coef, roi10mod_targdid_coef, roi11mod_targdid_coef, roi12mod_targdid_coef, roi13mod_targdid_coef, roi14mod_targdid_coef))

full_coef_data_targdid$sd <- full_coef_data_targdid$Std..Error * (sqrt(36))

write.csv(full_coef_data_targdid, "full_coef_data_targdid.csv")

full_coef_data_thirdhad<-data.frame(rbind(roi1mod_thirdhad_coef, roi2mod_thirdhad_coef, roi3mod_thirdhad_coef, roi4mod_thirdhad_coef, roi5mod_thirdhad_coef, roi6mod_thirdhad_coef, roi7mod_thirdhad_coef, roi8mod_thirdhad_coef, roi9mod_thirdhad_coef, roi10mod_thirdhad_coef, roi11mod_thirdhad_coef, roi12mod_thirdhad_coef, roi13mod_thirdhad_coef, roi14mod_thirdhad_coef))

full_coef_data_thirdhad$sd <- full_coef_data_thirdhad$Std..Error * (sqrt(36))

write.csv(full_coef_data_thirdhad, "full_coef_data_thirdhad.csv")

full_coef_data_thirddid<-data.frame(rbind(roi1mod_thirddid_coef, roi2mod_thirddid_coef, roi3mod_thirddid_coef, roi4mod_thirddid_coef, roi5mod_thirddid_coef, roi6mod_thirddid_coef, roi7mod_thirddid_coef, roi8mod_thirddid_coef, roi9mod_thirddid_coef, roi10mod_thirddid_coef, roi11mod_thirddid_coef, roi12mod_thirddid_coef, roi13mod_thirddid_coef, roi14mod_thirddid_coef))

full_coef_data_thirddid$sd <- full_coef_data_thirddid$Std..Error * (sqrt(36))

write.csv(full_coef_data_thirddid, "full_coef_data_thirddid.csv")

#Get the coefficients from each separate within condition data frame and combine them into a single df
full_coef_data=data.frame(cbind(full_coef_data_targhad$Estimate, full_coef_data_targdid$Estimate, full_coef_data_thirdhad$Estimate, full_coef_data_thirddid$Estimate))

#name the columns for each condition
colnames(full_coef_data)<-c("targhad","targdid","thirdhad","thirddid")

#specify an ROI variable that is the row number
full_coef_data = dplyr::mutate(full_coef_data, roi=row_number())

#gather the data so there is a column for condition and a column for slopes
full_coef_data<-gather(full_coef_data,"condition","slopes",1:4)

#specify that condition is a factor and set the levels
full_coef_data$condition=factor(full_coef_data$condition)

#recode condition so that targ had is the last level for helmert contrast
full_coef_data <- full_coef_data %>% mutate(
    condition_helm = case_when(
      condition=="targhad" ~ 4,
      condition=="targdid" ~ 3,
      condition=="thirdhad" ~ 2,
      condition=="thirddid" ~ 1,
    )
)
#specify that this new variable is a factor
full_coef_data$condition_helm=factor(full_coef_data$condition_helm)

#specify that the contrast to use for this variable is a helmert contrast
contrasts(full_coef_data$condition_helm) <- "contr.helmert"
#view contrast to double check
contrasts(full_coef_data$condition_helm)

#run regression
fullmodel<-lme4::lmer(slopes ~ condition_helm + (1|roi), data=full_coef_data)
summary(fullmodel)

noCondModel<-lme4::lmer(slopes ~ (1|roi), data=full_coef_data)
anova(fullmodel,noCondModel)

#compute descriptives within group
full_coef_desc <- ddply(full_coef_data, c("condition"), summarise,
               N    = length(slopes),
               mean = mean(slopes),
               sd   = sd(slopes),
               se   = sd / sqrt(N)
)
print(full_coef_desc)

#plot results: Bar graph
ggplot(full_coef_desc, aes(x=condition, y=mean)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2,
                 position=position_dodge(.9)) +
  ggtitle("Average slope of behavior predicted by classification accuracy") + 
  xlab("Condition") +
  ylab("Average Slope Across ROIs")

#plot results: Fancy Graph
cond_legend_title <- strwrap("ROI Number")
fig.all <- full_coef_data %>% mutate(roiNum = as.numeric(roi)) %>%
                ggplot(aes(x=condition, y=slopes)) +
                #ggtitle("Slopes from behavior predicted by classification accuracy") +
                geom_boxplot() +
                geom_point(aes(color=roi), size=6, position=position_jitterdodge(jitter.width = .5, seed=1), alpha = .2) +
                geom_hline(aes(yintercept = 0),linetype="dashed") +
                geom_text(aes(color=roi,label=roiNum),stat = "identity", fontface="bold", position=position_jitterdodge(jitter.width = .5, seed=1), alpha = 1, size=rel(3.25)) +
  scale_color_gradientn(cond_legend_title, colours = rainbow(5)) +
  scale_x_discrete(labels=str_wrap(c("Rational/Actual","Rational/Force","Irrational/Actual","Irrational/Force"), 10)) +
  guides(color = guide_colorbar(reverse = TRUE)) + 
                xlab("Condition") + 
                ylab(paste0("Strength of relationship between participants' difference score", "\n", "and classification accuracy within each ROI")) +
                theme_bw() +
                theme(strip.text = element_text(size=14),
                      axis.text = element_text(size=14),
                      plot.title = element_text(size=14, hjust=.5, face="bold"),
                      axis.title.x = element_text(size=16, vjust=-0.2),
                      axis.title.y = element_text(size=16, vjust=.2),
                      #legend.title = element_blank(),
                      #legend.text = element_text(size=16),
                      legend.position = "none",
                      #legend.direction = "vertical",
                      panel.grid = element_blank())

ggsave("fig.jpg",height = 8, width= 6.5, dpi=600)




```

#Generate a plot of classification accuracy predicted by behavior for each ROI in the had relevant condition specifically
```{r targ_had specific analyses}

#1. For each ROI, create an above chance accuracy measure

roi1_data$avg_targ_had_acc_above_chance <- roi1_data$avg_targ_had_acc -.5
roi2_data$avg_targ_had_acc_above_chance <- roi2_data$avg_targ_had_acc -.5
roi3_data$avg_targ_had_acc_above_chance <- roi3_data$avg_targ_had_acc -.5
roi4_data$avg_targ_had_acc_above_chance <- roi4_data$avg_targ_had_acc -.5
roi5_data$avg_targ_had_acc_above_chance <- roi5_data$avg_targ_had_acc -.5
roi6_data$avg_targ_had_acc_above_chance <- roi6_data$avg_targ_had_acc -.5
roi7_data$avg_targ_had_acc_above_chance <- roi7_data$avg_targ_had_acc -.5
roi8_data$avg_targ_had_acc_above_chance <- roi8_data$avg_targ_had_acc -.5
roi9_data$avg_targ_had_acc_above_chance <- roi9_data$avg_targ_had_acc -.5
roi10_data$avg_targ_had_acc_above_chance <- roi10_data$avg_targ_had_acc -.5
roi11_data$avg_targ_had_acc_above_chance <- roi11_data$avg_targ_had_acc -.5
roi12_data$avg_targ_had_acc_above_chance <- roi12_data$avg_targ_had_acc -.5
roi13_data$avg_targ_had_acc_above_chance <- roi13_data$avg_targ_had_acc -.5
roi14_data$avg_targ_had_acc_above_chance <- roi14_data$avg_targ_had_acc -.5

#Merge key variables into single dataset into single dataset
d <- as.data.frame(cbind(roi1_data$subj, roi1_data$difference_score, roi1_data$avg_targ_had_acc_above_chance, roi2_data$avg_targ_had_acc_above_chance, roi3_data$avg_targ_had_acc_above_chance, roi4_data$avg_targ_had_acc_above_chance, roi5_data$avg_targ_had_acc_above_chance, roi6_data$avg_targ_had_acc_above_chance, roi7_data$avg_targ_had_acc_above_chance, roi8_data$avg_targ_had_acc_above_chance, roi9_data$avg_targ_had_acc_above_chance, roi10_data$avg_targ_had_acc_above_chance, roi11_data$avg_targ_had_acc_above_chance, roi12_data$avg_targ_had_acc_above_chance, roi13_data$avg_targ_had_acc_above_chance, roi14_data$avg_targ_had_acc_above_chance))

colnames(d) <- c('subj', 'difference_score', 'roi1_RH_acc_above_chance', 'roi2_RH_acc_above_chance', 'roi3_RH_acc_above_chance', 'roi4_RH_acc_above_chance', 'roi5_RH_acc_above_chance', 'roi6_RH_acc_above_chance', 'roi7_RH_acc_above_chance', 'roi8_RH_acc_above_chance', 'roi9_RH_acc_above_chance', 'roi10_RH_acc_above_chance', 'roi11_RH_acc_above_chance', 'roi12_RH_acc_above_chance', 'roi13_RH_acc_above_chance', 'roi14_RH_acc_above_chance')

cor_plot <- ggplot(d)+
  geom_smooth(aes(difference_score,roi1_RH_acc_above_chance),color="#DC050C",method="lm",se=FALSE)+
  geom_smooth(aes(difference_score,roi2_RH_acc_above_chance),color="#E8601C",method="lm",se=FALSE)+
  geom_smooth(aes(difference_score,roi3_RH_acc_above_chance),color="#F1932D",method="lm",se=FALSE)+
  geom_smooth(aes(difference_score,roi4_RH_acc_above_chance),color="#F6C141",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi5_RH_acc_above_chance),color="#F7F056",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi6_RH_acc_above_chance),color="#CAE0AB",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi7_RH_acc_above_chance),color="#90C987",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi8_RH_acc_above_chance),color="#4EB265",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi9_RH_acc_above_chance),color="#7BAFDE",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi10_RH_acc_above_chance),color="#5289C7",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi11_RH_acc_above_chance),color="#1965B0",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi12_RH_acc_above_chance),color="#882E72",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi13_RH_acc_above_chance),color="#AE76A3",method="lm",se=FALSE) +
  geom_smooth(aes(difference_score,roi14_RH_acc_above_chance),color="#D1BBD7",method="lm",se=FALSE) +
  xlab("Difference Score") + 
  ylab("Classification Accuracy Above Chance") +
                theme_bw() +
                theme(strip.text = element_text(size=14),
                      axis.text = element_text(size=14),
                      plot.title = element_text(size=14, hjust=.5, face="bold"),
                      axis.title.x = element_text(size=14, vjust=-0.2),
                      axis.title.y = element_text(size=14, vjust=0.35),
                      legend.title = element_blank(),
                      legend.text = element_text(size=16),
                      legend.position = "bottom",
                      legend.direction = "vertical",
                      panel.grid = element_blank())

cor_plot + geom_hline(yintercept=0)
  

```

#Visualize accuracy predicting difference score within each condition
```{r graphs targ had}

#plot residuals predicted by action and condition
ggplot(roi1_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi2_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi3_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi4_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi5_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi6_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi7_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi8_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi9_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi10_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi11_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi12_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi13_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(roi14_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

ggplot(allrois_allconds_acc,aes(y=accuracies,x=difference_score, color=condition))+
  geom_point()+
  geom_smooth(method=lm)

```

###Exploratory Analyses###
```{r test signficance of classification accuracies for targ had}
mosaic::t.test(~avg_targ_had_acc, data=roi1_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi2_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi3_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi4_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi5_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi6_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi7_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi8_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi9_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi10_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi11_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi12_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi13_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=roi14_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_had_acc, data=allrois_data, mu=.5, alternative="greater")



```

```{r test signficance of classification accuracies for targ did}
mosaic::t.test(~avg_targ_did_acc, data=roi1_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi2_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi3_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi4_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi5_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi6_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi7_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi8_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi9_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi10_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi11_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi12_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi13_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=roi14_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_targ_did_acc, data=allrois_data, mu=.5, alternative="greater")

```

```{r test signficance of classification accuracies for third had}
mosaic::t.test(~avg_third_did_acc, data=roi1_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi2_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi3_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi4_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi5_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi6_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi7_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi8_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi9_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi10_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi11_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi12_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi13_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=roi14_data, mu=.5, alternative="greater")
mosaic::t.test(~avg_third_did_acc, data=allrois_data, mu=.5, alternative="greater")

```


```{r ttest targ had}

#combine regression coeffiecients for each model into a single dataset
full_coef_data_targhad<-data.frame(rbind(roi1mod_coef, roi2mod_coef, roi3mod_coef, roi4mod_coef, roi5mod_coef, roi6mod_coef, roi7mod_coef, roi8mod_coef, roi9mod_coef, roi10mod_coef, roi11mod_coef, roi12mod_coef, roi13mod_coef, roi14mod_coef))

mosaic::t.test(~Estimate, data=full_coef_data_targhad, mu=0)

hist(full_coef_data_targhad$Estimate, 
     main="Behavior Predicted by Activation Across 14 ROIs", 
     xlab="Slopes")
```

```{r ttest targ did}

#combine regression coeffiecients for each model into a single dataset
full_coef_data_targdid<-data.frame(rbind(roi1mod_targdid_coef, roi2mod_targdid_coef, roi3mod_targdid_coef, roi4mod_targdid_coef, roi5mod_targdid_coef, roi6mod_targdid_coef, roi7mod_targdid_coef, roi8mod_targdid_coef, roi9mod_targdid_coef, roi10mod_targdid_coef, roi11mod_targdid_coef, roi12mod_targdid_coef, roi13mod_targdid_coef, roi14mod_targdid_coef))

mosaic::t.test(~Estimate, data=full_coef_data_targdid, mu=0)

hist(full_coef_data_targdid$Estimate, 
     main="Behavior Predicted by Activation Across 14 ROIs", 
     xlab="slopes")
```

```{r ttest third had}

#combine regression coeffiecients for each model into a single dataset
full_coef_data_thirdhad<-data.frame(rbind(roi1mod_thirdhad_coef, roi2mod_thirdhad_coef, roi3mod_thirdhad_coef, roi4mod_thirdhad_coef, roi5mod_thirdhad_coef, roi6mod_thirdhad_coef, roi7mod_thirdhad_coef, roi8mod_thirdhad_coef, roi9mod_thirdhad_coef, roi10mod_thirdhad_coef, roi11mod_thirdhad_coef, roi12mod_thirdhad_coef, roi13mod_thirdhad_coef, roi14mod_thirdhad_coef))

mosaic::t.test(~Estimate, data=full_coef_data_thirdhad, mu=0)

hist(full_coef_data_thirdhad$Estimate, 
     main="Behavior Predicted by Activation Across 14 ROIs", 
     xlab="slopes")
```

```{r ttest third did}

#combine regression coeffiecients for each model into a single dataset
full_coef_data_thirddid<-data.frame(rbind(roi1mod_thirddid_coef, roi2mod_thirddid_coef, roi3mod_thirddid_coef, roi4mod_thirddid_coef, roi5mod_thirddid_coef, roi6mod_thirddid_coef, roi7mod_thirddid_coef, roi8mod_thirddid_coef, roi9mod_thirddid_coef, roi10mod_thirddid_coef, roi11mod_thirddid_coef, roi12mod_thirddid_coef, roi13mod_thirddid_coef, roi14mod_thirddid_coef))

mosaic::t.test(~Estimate, data=full_coef_data_thirddid, mu=0)

hist(full_coef_data_thirddid$Estimate, 
     main="Behavior Predicted by Activation Across 14 ROIs", 
     xlab="slopes")
```